<?php
/**
 *
 * (model)
 */
namespace App\Models\SalesReportLT;

use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
use App\Util\User;
class PaymentDetail extends Model
{
    protected $table = 'lt_sr_payment_detail';

    protected $guarded = [];

    public $timestamps = false;

    public function sku(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function classify(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public static function headersField(): array
    {
        $fields = [];
        $fields['report_date'] = ['title' => '日期','sumField' => false];
        $fields['sku'] = ['title' => 'SKU','sumField' => false];
        $fields['sku_cn'] = ['title' => '产品名称','sumField' => false];
        $fields['season'] = ['title' => '季节性','sumField' => false];
        $fields['classify'] = ['title' => '品类','sumField' => false];
        $fields['seller'] = ['title' => '销售','sumField' => false];
        $fields['rate_odds'] = ['title' => '汇率','sumField' => false];
        $fields['sales_volume'] = ['title' => '销量','sumField' => true];
        $fields['sales_amount'] = ['title' => '销售额','sumField' => false];
        $fields['sales_amount_rmb'] = ['title' => '销售额RMB','sumField' => true];
        $fields['refund_num'] = ['title' => '退货数量','sumField' => true];
        $fields['refund_amount'] = ['title' => '退款金额','sumField' => false];
        $fields['refund_amount_rmb'] = ['title' => '退款金额RMB','sumField' => true];
        $fields['thfba'] = ['title' => '三方发货费','sumField' => false];
        $fields['thfba_rmb'] = ['title' => '三方发货费RMB','sumField' => true];
        $fields['purchase_price_rmb'] = ['title' => '采购成本RMB','sumField' => true];
        $fields['t_purchase_price_rmb'] = ['title' => '总采购成本RMB','sumField' => true];
        $fields['t_shipment_price_rmb'] = ['title' => '总运费成本RMB','sumField' => true];
        $fields['other_fee_rmb'] = ['title' => '额外费用RMB','sumField' => true];
        //------------新加字段
        $fields['system_use_pc_fee'] = ['title' => '系统利用费_PC','sumField' => false];
        $fields['system_use_pc_fee_rmb'] = ['title' => '系统利用费_PCRMB','sumField' => true];
        $fields['system_use_sp_fee'] = ['title' => '系统利用费_SP','sumField' => false];
        $fields['system_use_sp_fee_rmb'] = ['title' => '系统利用费_SPRMB','sumField' => true];
        $fields['system_use_c_fee'] = ['title' => '系统利用费_共通','sumField' => false];
        $fields['system_use_c_fee_rmb'] = ['title' => '系统利用费_共通RMB','sumField' => true];
        $fields['super_membership_rewards_fee'] = ['title' => '超级会员_成果报酬','sumField' => false];
        $fields['super_membership_rewards_fee_rmb'] = ['title' => '超级会员_成果报酬RMB','sumField' => true];
        $fields['super_membership_system_use_fee'] = ['title' => '超级会员_系统利用费','sumField' => false];
        $fields['super_membership_system_use_fee_rmb'] = ['title' => '超级会员_系统利用费RMB','sumField' => true];
        $fields['super_membership_selling_fee'] = ['title' => '超级会员_佣金','sumField' => false];
        $fields['super_membership_selling_fee_rmb'] = ['title' => '超级会员_佣金RMB','sumField' => true];
        $fields['super_membership_selling_re_fee'] = ['title' => '超级会员_佣金减免','sumField' => false];
        $fields['super_membership_selling_re_fee_rmb'] = ['title' => '超级会员_佣金减免RMB','sumField' => true];
        $fields['integral_pc_fee'] = ['title' => '积分_PC','sumField' => false];
        $fields['integral_pc_fee_rmb'] = ['title' => '积分_PCRMB','sumField' => true];
        $fields['integral_sp_fee'] = ['title' => '积分_SP','sumField' => false];
        $fields['integral_sp_fee_rmb'] = ['title' => '积分_SPRMB','sumField' => true];
        $fields['rakuten_pay_use_fee'] = ['title' => 'RakutenPay利用费','sumField' => false];
        $fields['rakuten_pay_use_fee_rmb'] = ['title' => 'RakutenPay利用费RMB','sumField' => true];
        $fields['rakuten_pay_visa_fee'] = ['title' => 'RakutenPay_VISA支付费','sumField' => false];
        $fields['rakuten_pay_visa_fee_rmb'] = ['title' => 'RakutenPay_VISA支付费RMB','sumField' => true];
        $fields['service_fee'] = ['title' => '服务费','sumField' => false];
        $fields['service_fee_rmb'] = ['title' => '服务费RMB','sumField' => true];
        $fields['spend'] = ['title' => '广告费用合计','sumField' => false];
        $fields['spend_rmb'] = ['title' => '广告费用合计RMB','sumField' => true];
        $fields['reimburse_rmb'] = ['title' => '刷单费用RMB','sumField' => true];
        $fields['reimburse_os_rmb'] = ['title' => '运营服务费RMB','sumField' => true];
        $fields['reimburse_profit_rmb'] = ['title' => '刷单毛利扣减RMB','sumField' => true];
        $fields['profit_rmb'] = ['title' => '毛利值RMB','sumField' => true];
        $fields['profit_rate'] = ['title' => '毛利率','sumField' => false];
        $fields['earning_rmb'] = ['title' => '销售收入RMB','sumField' => true];
        $fields['total'] = ['title' => 'Total','sumField' => false];
        $fields['total_rmb'] = ['title' => 'TotalRMB','sumField' => true];
        $fields['total_rate'] = ['title' => '回款率','sumField' => false];
        return $fields;
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [];
            $temp['report_date'] = $row->report_date;
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            $temp['season'] = ProductClassify::$_season[$row->classify->season ?? 3];
            $temp['classify'] = $row->classify->title ?? '';
            $temp['seller'] = User::getUsernameById($row->seller_id);
            $temp['rate_odds'] = $row->rate_odds;
            $temp['sales_volume'] = $row->sales_volume;
            $temp['sales_amount'] = $row->sales_amount;
            $temp['sales_amount_rmb'] = $row->sales_amount_rmb;
            $temp['refund_num'] = $row->refund_num;
            $temp['refund_amount'] = $row->refund_amount;
            $temp['refund_amount_rmb'] = $row->refund_amount_rmb;
            $temp['thfba'] = $row->thfba;
            $temp['thfba_rmb'] = $row->thfba_rmb;
            $temp['purchase_price_rmb'] = $row->purchase_price_rmb;
            $temp['t_purchase_price_rmb'] = $row->t_purchase_price_rmb;
            $temp['t_shipment_price_rmb'] = $row->t_shipment_price_rmb;
            $temp['other_fee_rmb'] = $row->other_fee_rmb;
            //------------新加字段
            $temp['system_use_pc_fee'] = $row->system_use_pc_fee;
            $temp['system_use_pc_fee_rmb'] = $row->system_use_pc_fee_rmb;
            $temp['system_use_sp_fee'] = $row->system_use_sp_fee;
            $temp['system_use_sp_fee_rmb'] = $row->system_use_sp_fee_rmb;
            $temp['system_use_c_fee'] = $row->system_use_c_fee;
            $temp['system_use_c_fee_rmb'] = $row->system_use_c_fee_rmb;
            $temp['super_membership_rewards_fee'] = $row->super_membership_rewards_fee;
            $temp['super_membership_rewards_fee_rmb'] = $row->super_membership_rewards_fee_rmb;
            $temp['super_membership_system_use_fee'] = $row->super_membership_system_use_fee;
            $temp['super_membership_system_use_fee_rmb'] = $row->super_membership_system_use_fee_rmb;
            $temp['super_membership_selling_fee'] = $row->super_membership_selling_fee;
            $temp['super_membership_selling_fee_rmb'] = $row->super_membership_selling_fee_rmb;
            $temp['super_membership_selling_re_fee'] = $row->super_membership_selling_re_fee;
            $temp['super_membership_selling_re_fee_rmb'] = $row->super_membership_selling_re_fee_rmb;
            $temp['integral_pc_fee'] = $row->integral_pc_fee;
            $temp['integral_pc_fee_rmb'] = $row->integral_pc_fee_rmb;
            $temp['integral_sp_fee'] = $row->integral_sp_fee;
            $temp['integral_sp_fee_rmb'] = $row->integral_sp_fee_rmb;
            $temp['rakuten_pay_use_fee'] = $row->rakuten_pay_use_fee;
            $temp['rakuten_pay_use_fee_rmb'] = $row->rakuten_pay_use_fee_rmb;
            $temp['rakuten_pay_visa_fee'] = $row->rakuten_pay_visa_fee;
            $temp['rakuten_pay_visa_fee_rmb'] = $row->rakuten_pay_visa_fee_rmb;
            $temp['service_fee'] = $row->service_fee;
            $temp['service_fee_rmb'] = $row->service_fee_rmb;
            $temp['spend'] = $row->spend;
            $temp['spend_rmb'] = $row->spend_rmb;
            $temp['reimburse_rmb'] = $row->reimburse_rmb;
            $temp['reimburse_os_rmb'] = $row->reimburse_os_rmb;
            $temp['reimburse_profit_rmb'] = $row->reimburse_profit_rmb;
            $temp['profit_rmb'] = $row->profit_rmb;
            $temp['profit_rate'] = getPercentage($row->profit_rmb,$row->sales_amount_rmb);
            $temp['earning_rmb'] = $row->earning_rmb;
            $temp['total'] = $row->total;
            $temp['total_rmb'] = $row->total_rmb;
            $temp['total_rate'] = getPercentage($row->total_rmb,$row->sales_amount_rmb);
            yield $temp;
        }
    }
}
